/**
 * Project Name:fastPlat
 * File Name:AttachFilesController.java
 * Package Name:com.fast.cms.web.controller.article
 * Date:2017年06月04日 21时40分21秒
 * Copyright (c) 2017, lsflu@126.com All Rights Reserved.
 *
*/

package com.fast.cms.web.controller.article;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.fast.base.db.page.PageView;
import com.fast.base.db.sql.AbstractSqlGenerator;
import com.fast.base.model.AnoSysLog;
import com.fast.base.util.ChineseSpelling;
import com.fast.base.util.LogUtils;
import com.fast.base.util.StringUtils;
import com.fast.base.util.data.MathUtils;
import com.fast.base.util.web.ResponseData;
import com.fast.base.util.web.ResponseEnum;
import com.fast.base.web.BaseController;
import com.fast.cms.model.article.AttachFiles;
import com.fast.cms.service.article.AttachFilesService;
import com.fast.cms.web.form.AttachFilesForm;
import com.fast.common.userCache.UserRequestUtils;

/**
 * ClassName:AttachFilesController <br/>
 * 用途: AttachFiles web控制类 <br/>
 * 开发人员：lushuifa  http://lushuifa.iteye.com/
 * 邮箱:lsflu@126.com
 * @version  1.0
 * @since    JDK 1.6
 * @see
 * Date:     2017年06月04日 21时40分21秒 <br/> 	 
 */
@Controller
@RequestMapping("/attachFiles")
public class AttachFilesController extends BaseController{
	
	@Resource(name="attachFilesService")
	private AttachFilesService attachFilesService;

	/**
	 * 编辑AttachFiles信息
	 * @param imageUrl
	 * @return
	 */
	@RequestMapping("/editAttachFiles.do")
	@ResponseBody
	@AnoSysLog(actionName="编辑AttachFiles信息")
	public ResponseData editAttachFiles(@RequestParam MultipartFile attachFile){
		ResponseData responseData = new ResponseData();
		AttachFiles attachFiles = null;
		try {
			String uuid = webUtil.getString("uuid");
			if(StringUtils.isEmpty(uuid)){
				uuid = MathUtils.getTheOnlyStr();
			}
			String attachFilePath = "";
			String originalFileName = "";
			String fileExt = "";
			long fileSize = 0L;
			if(!attachFile.isEmpty()&&attachFile.getSize()>0){
				try {
					fileSize = attachFile.getSize();
					originalFileName = attachFile.getOriginalFilename();
					String savePath  = super.getFileDiskPath()+"doc/";
					String fileName  = uuid+ChineseSpelling.getSpell(attachFile.getOriginalFilename());
					attachFilePath = "doc/"+fileName;
					File saveFile = new File(savePath,fileName);
				    FileUtils.copyInputStreamToFile(attachFile.getInputStream(), saveFile);
				    fileExt=fileName.substring(fileName.lastIndexOf(".")+1);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			
			int id = webUtil.getInt("id");
			if(id>0){
				attachFiles = this.attachFilesService.getById(AttachFiles.class,id);
				attachFiles = (AttachFiles) webUtil.bindDataToBean(attachFiles);
			}else{
				attachFiles = new AttachFiles();
				attachFiles = (AttachFiles) webUtil.bindDataToBean(attachFiles);
				attachFiles.setFilePath(attachFilePath);
				attachFiles.setFileName(originalFileName);
				attachFiles.setFileExt(fileExt);
				attachFiles.setFileSize(fileSize);
				UserRequestUtils.bindCreaterInfo(attachFiles);
			}
			UserRequestUtils.bindEditorInfo(attachFiles);
			AttachFiles resultBean = this.attachFilesService.merge(attachFiles);
			if(resultBean!=null){
			    responseData.setData(resultBean);
				responseData.buildResult(ResponseEnum.EDIT_SUCCESS);
			}else{
				responseData.buildResult(ResponseEnum.EDIT_FAILUE);
			}
		} catch (Exception e) {
			e.printStackTrace();
			responseData.failue();
		}
		return responseData;
	}
	
	
	/**
	 * 
	 * 说明: <br>
	 * 编写者:lushuifa
	 * 创建时间:2017年9月22日 下午11:17:38 </pre>
	 * @param 
	 * @return ResponseData 说明
	 */
	@ResponseBody
	@RequestMapping("/updateDownLoadFlag.do")
	@AnoSysLog(actionName="更新文件是否下载的状态值")
	public ResponseData updateDownLoadFlag(int id,boolean isDownload) {
		LogUtils.info("updateDownLoadFlag id=:"+id);
		ResponseData responseData = new ResponseData();
		try {
			String sql = "update "+AbstractSqlGenerator.getTableName(AttachFiles.class)+" set isDownload="+isDownload+" where id="+id;
			boolean actionFlag = attachFilesService.modifyRow(sql)>0;
			responseData.setCode(actionFlag?"00":"01");
			if(actionFlag){
				responseData.buildSuccessResponse();
			}else{
				responseData.failue();
			}
		} catch (Exception e) {
			e.printStackTrace();
			responseData.failue();
		}
		LogUtils.info("updateDownLoadFlag responseData=:"+responseData.toString());
		return responseData;
	}
	
	/**
	 * 删除AttachFiles信息到回收站
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/delAttachFilesByIdForHS.do")
	@AnoSysLog(actionName="删除AttachFiles到回收站")
	public ResponseData delAttachFilesByIdForHS(int id) {
		LogUtils.info("delAttachFilesByIdForHS id=:"+id);
		ResponseData responseData = new ResponseData();
		try {
			String sql = "update "+AbstractSqlGenerator.getTableName(AttachFiles.class)+" set delete_Flag=1 where id="+id;
			boolean actionFlag = attachFilesService.modifyRow(sql)>0;
			responseData.setCode(actionFlag?"00":"01");
			if(actionFlag){
				responseData.buildResult(ResponseEnum.DELETE_SUCCESS);
			}else{
				responseData.buildResult(ResponseEnum.DELETE_FAILUE);
			}
		} catch (Exception e) {
			e.printStackTrace();
			responseData.failue();
		}
		LogUtils.info("delAttachFilesByIdForHS responseData=:"+responseData.toString());
		return responseData;
	}

	/**
	 * 删除AttachFiles信息
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/delAttachFilesById.do")
	@AnoSysLog(actionName="彻底删除AttachFiles")
	public ResponseData delAttachFilesById(int id) {
		LogUtils.info("delAttachFilesById id=:"+id);
		ResponseData responseData = new ResponseData();
		try {
			boolean actionFlag = attachFilesService.deleteById(AttachFiles.class, id);
			responseData.setCode(actionFlag?"00":"01");
			if(actionFlag){
				responseData.buildResult(ResponseEnum.DELETE_SUCCESS);
			}else{
				responseData.buildResult(ResponseEnum.DELETE_FAILUE);
			}
		} catch (Exception e) {
			e.printStackTrace();
			responseData.failue();
		}
		LogUtils.info("delAttachFilesById responseData=:"+responseData.toString());
		return responseData;
	}
	
	/**
	 * 删除多个AttachFiles信息
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/batchDelAttachFiles.do")
	@AnoSysLog(actionName="批量删除AttachFiles信息")
	public ResponseData batchDelAttachFiles(String ids){
		ResponseData responseData = new ResponseData();
		try {
			int actionCount = attachFilesService.deleteByIds(AttachFiles.class, ids);
			if(actionCount>0){
				responseData.buildResult(ResponseEnum.DELETE_SUCCESS);
			}else{
				responseData.buildResult(ResponseEnum.DELETE_FAILUE);
			}
		} catch (Exception e) {
			e.printStackTrace();
			responseData.failue();
		}
		return responseData;
	}
	
	/**
	 * 删除多个AttachFiles信息到回收站
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/batchDelAttachFilesForHS.do")
	@AnoSysLog(actionName="批量删除AttachFiles")
	public ResponseData batchDelAttachFilesForHS(String ids){
		ResponseData responseData = new ResponseData();
		try {
			boolean actionFlag = attachFilesService.batchDeleteByIdsForHS(AttachFiles.class, ids);
			if(actionFlag){
				responseData.buildResult(ResponseEnum.DELETE_SUCCESS);
			}else{
				responseData.buildResult(ResponseEnum.DELETE_FAILUE);
			}
		} catch (Exception e) {
			e.printStackTrace();
			responseData.failue();
		}
		return responseData;
	}
	
	/**
	 * 分页得到AttachFiles列表
	 * @return PageView<AttachFiles>
	 * @throws IOException 
	 */
	@ResponseBody
	@RequestMapping("/getAttachFilesList.do")
	public Object getAttachFilesList(AttachFilesForm attachFilesForm){
		LogUtils.info("getAttachFilesList param:"+attachFilesForm.toString());
		Map<String,Object> gridMap = new HashMap<String,Object>();
		PageView<AttachFiles> pageView = null;
		try {
			pageView = this.attachFilesService.getAttachFilesList(attachFilesForm);
			gridMap.put("total", pageView.getTotalRecord());
			gridMap.put("rows", pageView.getRecords());
		} catch (Exception e) {
			LogUtils.error("getAttachFilesList err:",e);
		}
		LogUtils.info("getAttachFilesList result:"+pageView.toString());
 		return gridMap;
	}
	
	/**
	 * 获取AttachFiles详情
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/js/getAttachFilesDetail.do")
	public ResponseData getAjaxAttachFilesDetail(int id) {
		LogUtils.info("getAttachFilesDetail is comming!");
		ResponseData responseData = new ResponseData();
		try {
			AttachFiles attachFiles = attachFilesService.getById(AttachFiles.class,id);
			responseData.setData(attachFiles);
		} catch (Exception e) {
			e.printStackTrace();
			LogUtils.info("获取AttachFiles详情getAttachFilesDetail err",e);
			responseData.failue();
		}
		return responseData;
	}
	
	/**
	 * 查询AttachFiles详情
	 * @param id
	 * @return
	 */
	@RequestMapping("/getAttachFilesDetail.do")
	public ModelAndView getAttachFilesDetail(int id,String actionType) {
		ModelAndView mv = new ModelAndView();
		AttachFiles bean = attachFilesService.queryForBeanById(AttachFiles.class, id);
		try {
			mv.addObject("attachFiles",bean);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if(StringUtils.equals(actionType,"update")){
			mv.setViewName("/adminMain/attachFiles/attachFiles-add.jsp");
		}else{
			mv.setViewName("/adminMain/attachFiles/attachFiles-detail.jsp");
		}
		return mv;
	}
}

